package dao;

import entity.SaleDetails;
import entity.Supplier;
import entity.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class SaleDetailsDao extends BaseDao {
    //列表所有销售单明细信息
    public List getAllSaleDetails() throws Exception {
        List saleDetailList = new ArrayList();
        String sql = "select saleDetailsId,saleId,productId,saleNumber" +
                " from saleDetails";
        Connection connection = getConnection();//获取数据库连接
        Statement statement = connection.createStatement();//创建statement对象
        ResultSet resultSet = statement.executeQuery(sql);//执行查询sql
        while(resultSet.next()){
            String saleDetailsId = resultSet.getString("saleDetailsId");
            String saleId = resultSet.getString("saleId");
            String productId = resultSet.getString("productId");
            int saleNumber = resultSet.getInt("saleNumber");
            SaleDetails saleDetails = new SaleDetails(saleDetailsId,saleId,productId,saleNumber);
            saleDetailList.add(saleDetails);
        }
        return saleDetailList;
    }

    //录入订单明细信息
    public boolean addSaleDetail(SaleDetails saleDetails) throws Exception {
        String sql = "insert into saleDetails(saleDetailsId,saleId,productId,saleNumber)" +
                " VALUES(?,?,?,?)";
        Connection connection = getConnection();//获取数据库连接
        PreparedStatement preparedStatement = connection.prepareStatement(sql);//创建PreparedStatement
        preparedStatement.setString(1, saleDetails.getSaleDetailsId());
        //设置第一个？占位符的值
        preparedStatement.setString(2, saleDetails.getSaleId());
        //设置第二个？占位符的值
        preparedStatement.setString(3, saleDetails.getProductId());
        //设置第三个？占位符的值
        preparedStatement.setInt(4, saleDetails.getSaleNumber());
        //设置第四个？占位符的值

        int num = preparedStatement.executeUpdate();//执行sql语句
        if (num==1)
        {
            return true;
        }else {
            return false;
        }
    }

    //列表指定订单下的订单明细信息
    public List getAppointSaleDeatils(String saleIdTemp) throws Exception {
        List saleDetailsList = new ArrayList();
        String sql = "select saleDetailsId,saleId,productId,saleNumber" +
                " from saledetails where saleId='" + saleIdTemp + "'";
        Connection connection = getConnection();//获取数据库连接
        Statement statement = connection.createStatement();//创建statement对象
        ResultSet resultSet = statement.executeQuery(sql);//执行查询sql
        //遍历查询结果集
        while(resultSet.next()){
            String saleDetailsId = resultSet.getString("saleDetailsId");
            String saleId = resultSet.getString("saleId");
            String productId = resultSet.getString("productId");
            int saleNumber = resultSet.getInt("saleNumber");

            SaleDetails saleDetails = new SaleDetails(saleDetailsId,saleId,productId,saleNumber);
            saleDetailsList.add(saleDetails);
        }
        return saleDetailsList;
    }

    //删除订单详情信息
    public String deleteSaleDetails(SaleDetails saleDetails) throws Exception {
        String result= null;
        Connection connection = getConnection();//获取数据库连接

        String sql = "delete from saleDetails where saleDetailsId=?";//定义sql语句
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,saleDetails.getSaleDetailsId());
        int num = preparedStatement.executeUpdate();//执行删除sql操作
        if(num==1){
            //删除成功
            result = "删除成功";
        }else {
            //删除失败
            result = "删除失败";
        }
        return result;
    }

    public static void main(String[] args) throws Exception {
        SaleDetailsDao saleDetailsDao = new SaleDetailsDao();
        List aaa = saleDetailsDao.getAppointSaleDeatils("111");
        System.out.println(aaa);
    }
}
